package it.acweb.pauseless.pauselessapp;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class BrowseSongsActivity extends Activity {

	//private xml_utils ut;
	private Activity activity;
	
	public void alert (String msg) {
		Toast.makeText(getApplicationContext(),
			      msg, Toast.LENGTH_LONG)
			      .show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_browse_songs);
		activity = this;
		
		Intent intent = getIntent();
		ListView listView = (ListView) findViewById(R.id.songList);
		
		//inizializzo xml_utils
		xml_utils.setContext(getApplicationContext());

		try {

            Document doc = xml_utils.get_main_document(R.raw.cd_list);
            
            Node node = xml_utils.get_cd(doc, intent.getStringExtra("cd"));
            
            Song[] songs = xml_utils.get_songs(node);
            
            SongsAdapter adapter = new SongsAdapter(getApplicationContext(), R.layout.activity_browse_songs_item, songs);
            listView.setAdapter(adapter);

		} catch (Exception e) {
            System.out.println("XML Pasing Exception = " + e);
        }

		/*//Song[] songs = (Song[])intent.getParcelableArrayExtra("songs");
		Bundle bundle = intent.getExtras();
		Song[] songs = (Song[])bundle.getParcelableArray("songs");
        //SongsAdapter adapter = new SongsAdapter(getApplicationContext(), R.layout.activity_browse_songs_item, songs);
        //listView.setAdapter(adapter);*/

        listView.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view,
			    int position, long id) {
				  
				  String[] choices = {"Aggiungi a mix", "Modifica", "bla", "bla"};
				  
				  AlertDialog.Builder builder = new AlertDialog.Builder(activity);
				  builder.setTitle("Titolo")
				  		 //.setMessage("Testo del dialog")
				         .setItems(choices, new DialogInterface.OnClickListener() {
				               public void onClick(DialogInterface dialog, int which) {
				                   // The 'which' argument contains the index position
				                   // of the selected item
				               }
				         });
				  AlertDialog dialog = builder.create();
				  dialog.show();
			  }
		}); 
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_browse_songs, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
